package com.azt.front.action.freemarker;

import com.azt.api.service.IntegralService;
import com.azt.front.utils.ContextUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.Writer;
import java.util.Map;


/**
 * 积分提示信息content(适用于无额外参数的情况)
 */
@Component("rule_content")
public class RuleContentDirective implements TemplateDirectiveModel {

	@Autowired
	private IntegralService integralService;

	@Override
	public void execute(Environment env, Map params, TemplateModel[] arg2, TemplateDirectiveBody body)
			throws TemplateException, IOException {
		String content = "";
		if(ContextUtil.getUserId() != null) {
			String code = DirectiveUtils.getString("code", params);        //积分规则

			if (StrUtil.isNotBlank(code)) {
				content = integralService.getRuleContent(code, ContextUtil.getUserId(), null);
			}
		}

		Writer out = env.getOut();
		out.append(content);
	}

}
